package com.goods.business.service.imp;

import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    //根据条件查询所有
    @Override
    public List findProductList(String name, Integer status, String categorys) {
        String[] split = StringUtils.split(categorys, ",");
        String onecategoryid = null;
        String twocategoryid = null;
        String threecategoryid = null;
        if(split != null && split.length>0){
            if(split.length==1){
                onecategoryid = split[0];

            }
            if(split.length==2){
                onecategoryid = split[0];
                twocategoryid = split[1];
            }
            if(split.length==3){
                onecategoryid = split[0];
                twocategoryid = split[1];
                threecategoryid = split[2];
            }
        }
        Map<Object, Object> map = new HashMap<>();
        map.put("name",name);
        map.put("status",status);
        map.put("onecategoryid",onecategoryid);
        map.put("twocategoryid",twocategoryid);
        map.put("threecategoryid",threecategoryid);
        List<ProductVO> list = productMapper.findProductList(map);
        return list;
    }

    @Override
    public void add(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        Long[] num = productVO.getCategoryKeys();
        int length = num.length;
        switch (length){
            case 3:
                product.setThreeCategoryId(num[2]);
            case 2:
                product.setTwoCategoryId(num[1]);
            case 1:
                product.setThreeCategoryId(num[0]);
        }
        product.setCreateTime(Calendar.getInstance().getTime());
        product.setModifiedTime(Calendar.getInstance().getTime());
        product.setPNum(UUID.randomUUID().toString());
        product.setStatus(2);
        productMapper.insert(product);
    }

    @Override
    public Product edit(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        Long[] num = productVO.getCategoryKeys();
        int length = num.length;
        switch (length){
            case 3:
                product.setThreeCategoryId(num[2]);
            case 2:
                product.setTwoCategoryId(num[1]);
            case 1:
                product.setThreeCategoryId(num[0]);
        }
        product.setModifiedTime(Calendar.getInstance().getTime());
        productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public void remove(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(1);
        product.setModifiedTime(Calendar.getInstance().getTime());
        productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public void back(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(0);
        product.setModifiedTime(Calendar.getInstance().getTime());
        productMapper.updateByPrimaryKeySelective(product);
    }

    //删除状态不知道
    //TODO
    @Override
    public void delete(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Boolean publish(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        if(product.getStatus() == 2) {
            product.setStatus(0);
            product.setModifiedTime(Calendar.getInstance().getTime());
            int i = productMapper.updateByPrimaryKeySelective(product);
            return i!=0;
        }
        return false;
    }

    @Override
    public List<ProductStockVO> findProductLists(String name, String categorys) {
        String[] split = StringUtils.split(categorys, ",");
        String onecategoryid = null;
        String twocategoryid = null;
        String threecategoryid = null;
        if(split != null && split.length>0){
            if(split.length==1){
                onecategoryid = split[0];
            }
            if(split.length==2){
                onecategoryid = split[0];
                twocategoryid = split[1];
            }
            if(split.length==3){
                onecategoryid = split[0];
                twocategoryid = split[1];
                threecategoryid = split[2];
            }
        }
        Map<Object, Object> map = new HashMap<>();
        map.put("name",name);
        map.put("onecategoryid",onecategoryid);
        map.put("twocategoryid",twocategoryid);
        map.put("threecategoryid",threecategoryid);
        List<ProductStockVO> list = productMapper.findProductStockVO(map);
        return list;


    }


}
